package br.unisinos.tetris.engine;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.SwingUtilities;

import br.unisinos.tetris.command.Command;
import br.unisinos.tetris.command.MoveDownCommand;
import br.unisinos.tetris.command.MoveLeftCommand;
import br.unisinos.tetris.command.MoveRightCommand;
import br.unisinos.tetris.command.RotateCommand;
import br.unisinos.tetris.factory.Phase1Factory;
import br.unisinos.tetris.factory.TetrisFactory;
import br.unisinos.tetris.state.PieceState;
import br.unisinos.tetris.view.TetrisApp;
import br.unisinos.tetris.view.TetrisView;

public class Controller implements KeyListener {

	private PieceState currentPiece;
	int count = 0;
	private PieceState nextPiece;
	private Command nextCommand;
	private Command moveDownCommand = new MoveDownCommand();
	private static TetrisFactory factory = new Phase1Factory();

	private static Controller theInstance = new Controller();

	public static Controller getInstance() {
		return theInstance;
	}

	private Controller() {
		currentPiece = factory.getRandomPiece();
		printNextPiece();
	}

	private void printNextPiece() {
		nextPiece = factory.getRandomPiece();
		TetrisView view = (TetrisView) TetrisApp.getApplication().getMainView();
		int currentX = nextPiece.getPiece().getX();
		nextPiece.getPiece().setX(55);
		nextPiece.getPiece().setY(30);
		nextPiece.paint(view.getNextPiecePanel());
		nextPiece.getPiece().setX(currentX);
		nextPiece.getPiece().setY(0);
		view.getNextPiecePanel().repaint();
	}

	public void start() {
		TetrisApp.getApplication().getMainView().getFrame().addKeyListener(this);
		doIt();
	}

	public void stop() {

	}

	public PieceState getCurrentPiece() {
		return currentPiece;
	}
	
	public void setCurrentPiece(PieceState state) {
		this.currentPiece = state;
	}

	public void doIt() {
		
		Thread appThread = new Thread() {
			public void run() {
				try {
					if (nextCommand != null) {
						SwingUtilities.invokeAndWait(nextCommand);
						nextCommand = null;
					}
					SwingUtilities.invokeAndWait(moveDownCommand);
					Thread.sleep(300);
					doIt();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
		if (count==24) {
			count = 0;
			this.currentPiece = nextPiece;
			this.nextPiece = factory.getRandomPiece();
			
			printNextPiece();
			
			count++;
		}else {
			count++;
		}
		appThread.start();
	}

	@Override
	public void keyPressed(KeyEvent keyEvent) {
		switch (keyEvent.getKeyCode()) {
		case KeyEvent.VK_LEFT:
			this.nextCommand = new MoveLeftCommand();
			break;
		case KeyEvent.VK_RIGHT:
			this.nextCommand = new MoveRightCommand();
			break;
		case KeyEvent.VK_UP: {
			this.nextCommand = new RotateCommand();
			break;
		}
		case KeyEvent.VK_DOWN: {
//			this.nextCommand = new MoveDownCommand();
//			this.count--;
			break;
		}
		default:
			break;
		}
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		
	}

}
